package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.support.CsunMessageSource;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.dto.wechat.BindMobileDto;
import com.csun.dsms.model.dto.wechat.WeChatLoginDto;
import com.csun.dsms.model.entity.TokenModel;
import com.csun.dsms.service.DsmsOpenUserService;
import com.csun.dsms.service.TokenManager;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-03 11:36
 * @description: 微信授权管理拦截器
 */
@RestController
@RequestMapping("/open")
public class DsmsOpenUserController {

    @Resource
    private DsmsOpenUserService dsmsOpenUserService;
    @Resource
    private TokenManager tokenManager;
    @Resource
    private CsunMessageSource csunMessageSource;

    /**
     * 微信小程序授权
     *
     * @param weChatLoginDto
     *
     * @return
     */
    @Authorization
    @PostMapping(value = "/wx/register")
    public Wrapper register(@CurrentUser DsmsUser login, WeChatLoginDto weChatLoginDto) {

        Integer result =  dsmsOpenUserService.register(weChatLoginDto, login);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 微信小程序取消授权
     *
     * @param login the login user
     *
     * @return
     */
    @Authorization
    @PostMapping(value = "/wx/unRegister")
    public Wrapper unrRegister(@CurrentUser DsmsUser login) {

        Integer result =  dsmsOpenUserService.unrRegister(login);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 微信小程序登录
     *
     * @param weChatLoginDto
     *
     * @return
     */
    @PostMapping(value = "/wx/login")
    public Wrapper login(WeChatLoginDto weChatLoginDto) {

        DsmsUser dsmsUser = dsmsOpenUserService.login(weChatLoginDto);

        //生成一个token，保存用户登录状态
        TokenModel model = tokenManager.get(dsmsUser.getId());
        if (model == null) {
            model = tokenManager.createToken(dsmsUser.getId());
        }

        return WrapMapper.ok(model);
    }

    /**
     * 绑定手机号
     *
     * @param bindMobileDto
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/bindMobile")
    public Wrapper bindMobile(BindMobileDto bindMobileDto) {

        Integer result = dsmsOpenUserService.bindMobile(bindMobileDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

}
